<?php

/*
  Project: Blogmer - Open Source Blogging Software
  Type: PHP
  Desc: Generate Tag Clouds for blog.
  Author: Yashpal
*/

class Tagclouds_model extends Model 
{
    function Tagclouds_model()
    { parent::Model();}
	
	//something is remaining
	//http://www.featurepics.com/editorial/tag-cloud.aspx
	//add color effect in tag cloud
	// tag_clouds()   -> Generate Tag Clouds for blog.
	function tag_clouds()
	{
			$temp="";//
			$query = $this->db->get('posts'); 
			foreach($query->result() as $row)
			{
			   $p = explode(',',$row->TagClouds);
			   for($i=0;$i<count($p);$i++)
			   	   	$temp[]=$p[$i];
			   
			}//end of foreach
			
			// Pull in tag data
			$tags=array_count_values($temp);
			
			//some part is taken from http://scriptplayground.com/tutorials/php/Tag-Cloud/
			// Default font sizes
			$min_font_size = 12;
			$max_font_size = 30;
			
			$minimum_count = min(array_values($tags));
			$maximum_count = max(array_values($tags));
			$spread = $maximum_count - $minimum_count;
			
			if($spread == 0) 
				$spread = 1;
			
			
			$cloud_html = '';
			$cloud_tags = array(); // create an array to hold tag code
			foreach ($tags as $tag => $count) 
			{
				$size = $min_font_size + ($count - $minimum_count) * ($max_font_size - $min_font_size) / $spread;
				if($tag!="") //removing empty tags //added by Yashpal
					$cloud_tags[] = anchor('/blog/search/tag/'.$tag,htmlspecialchars(stripslashes($tag)),array('title' => '\''.$tag.'\'returned a count of '.$count,'class'=>'tag_cloud','style'=>'font-size:'.floor($size).'px'));
			} //end of foreach
			
			return join("\n", $cloud_tags) . "\n";
	} //end of function
	

}//end of class	
?>